#pragma once
#include "Building.h"
class BuildingRegister : public vector<Building>	//Betyg 4
{
private:
	vector<Building>::iterator BVI;
	string brName;

public:

	BuildingRegister()
	{
		brName = " ";
	}

	BuildingRegister(string s)
	{
		brName = s;
	}


	friend ostream& operator<<(ostream& out, BuildingRegister& BR)
	{
		out << "MENU FOR BUILDINGREGISTER" << endl;
		out << "1. Add building" << endl;
		out << "2. Delete building" << endl;
		out << "3. Search for building" << endl;
		out << "4. List the buildings" << endl;
		out << "5. Calculate total area" << endl;
		out << "6. Read from file" << endl;
		out << "7. Write to file" << endl;
		out << "8. Exit" << endl;
		return out;
	}

	friend istream& operator>>(istream& in, BuildingRegister& BR)
	{
		cout << "Answer: ";

		string answer;
		in >> answer;

		if (answer == "1")
		{
			BR.NewBuilding();
		}

		else if (answer == "2")
		{
			BR.Delete();
		}

		else if (answer == "3")
		{
			BR.Search();
		}

		else if (answer == "4")
		{
			BR.PrintRegister(cout);
		}

		else if (answer == "5")
		{
			BR.Area();
		}

		else if (answer == "6")
		{
			string fileName;
			cout << "Name the file you want to open: ";
			cin >> fileName;
			BR.ReadFromFile(fileName);
		}

		else if (answer == "7")
		{
			string fileName;
			cout << "Name the file you want to open: ";
			cin >> fileName;
			BR.WriteToFile(fileName);
		}

		else if (answer == "8")
		{
			exit(EXIT_SUCCESS);
		}

		return in;
	}

	void NewBuilding()
	{
		Building temp;
		cout << endl << "Add building" << endl;
		cout << "(id,info)" << endl;
		cin >> temp;

		cout << "Add rooms to building (type 'e' to exit):" << endl;
		string s;
		cin >> s;

		while (s != "e")
		{
			temp.AddRoom();
			cout << "Add another room to the building. (type 'e' to exit):" << endl;
			cin >> s;
		}

		push_back(temp);
	}

	void NewBuilding(Building b)
	{
		push_back(b);
	}

	void Delete()
	{
		int id;
		cout << endl << "Delete building";
		cout << "ID: ";
		cin >> id;
		Building tempbuilding;
		tempbuilding.SetID(id);

		auto BVI = find(begin(), end(), tempbuilding);
		if (BVI != end())
		{
			erase(BVI, BVI + 1);
		}
	}

	void Search()
	{
		int id;
		cout << "ID: ";
		cin >> id;
		Building tempbuilding;
		tempbuilding.SetID(id);

		auto BVI = find(begin(), end(), tempbuilding);
		if (BVI != end())
		{
			cout << *BVI << endl;
		}

	}

	void Area()
	{
		double totalArea = 0;
		for (auto BVI = begin(); BVI < end(); BVI++)
		{
			if (BVI->GetID() != -1)
			{
				totalArea += BVI->AreaTot();
			}
		}

		cout << "Total area for all buildings: " << totalArea << "m2" << endl;
	}

	void PrintRegister(ostream &out)
	{
		out << "BuldingRegister= " << this->brName << endl;
		for (int i = 0; i < size(); i++)
		{
			out << "Building= " << (*this)[i] << endl;
			for (int j = 0; j < (*this)[i].GetRooms().size(); j++)
			{
				out << "Room= " << (*this)[i].PrintRoom(j);
			}
			out << endl;
		}
	}

	void WriteToFile(string fileName)
	{
		ofstream fout;
		fout.open(fileName);
		PrintRegister(fout);
		fout.close();
	}

	void ReadFromFile(string fileName)
	{
		string str, strRow;
		ifstream fin;

		try
		{
			fin.open(fileName);
			clear();

			Building b;
			Room r;


			while (getline(fin, strRow))
			{
				istringstream iss(strRow);

				getline(iss, str, '=');

				if (str.compare("BuildingRegister") == 0)
				{
					getline(iss, brName);
				}
				else if (str.compare("Building") == 0)
				{
					if (b.GetID() != -1) { this->NewBuilding(b); }
					b = Building();
					iss >> b;
				}
				else if (str.compare("Room") == 0)
				{
					r = Room();
					iss >> r;
					if (r.GetID() != -1) { b.Add(r); }
				}
			}
			if (b.GetID() != -1) { this->NewBuilding(b); }
			fin.close();
		}

		catch (exception e)
		{
			cerr << "Exception: " << fileName << endl;
			cerr << e.what() << endl;
		}
	}

	~BuildingRegister()
	{
	}
};

